package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetMealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
@Slf4j
@Service
public class SetMealServiceimpl implements SetMealService {
    @Autowired
    SetmealMapper setmealMapper;
    @Autowired
    SetMealDishMapper setMealDishMapper;
    @Autowired
    DishMapper dishMapper;
    @Transactional
    @Override
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmeal.setStatus(0);
        //添加套餐表
        setmealMapper.save(setmeal);
        //添加套餐菜品表
        Long setmealId=setmeal.getId();
        List<SetmealDish> setmealDishes=setmealDTO.getSetmealDishes();
        if (setmealDishes!=null&&setmealDishes.size()!=0)
        {
            setmealDishes.forEach(setmealDish -> {
                setmealDish.setSetmealId(setmealId);
            });
            setMealDishMapper.save(setmealDishes);
        }
    }
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page=setmealMapper.page(setmealPageQueryDTO);
        List<SetmealVO> list=page.getResult();
        Long total=page.getTotal();
        return new PageResult(total,list);
    }
    @Transactional
    @Override
    public void update(SetmealDTO setmealDTO) {
    Setmeal setmeal=new Setmeal();
    BeanUtils.copyProperties(setmealDTO,setmeal);
    //修改套餐表
    setmealMapper.update(setmeal);
    //修改菜品和套餐关联表
        List<SetmealDish> setmealDishes=setmealDTO.getSetmealDishes();
        List<Long> ids=new ArrayList<>();
        ids.add(setmealDTO.getId());
        if (setmealDishes!=null&&setmealDishes.size()!=0)
        {   setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealDTO.getId());
        });
            setMealDishMapper.deleteByMealId(ids);
            setMealDishMapper.save(setmealDishes);
        }
    }
    @Override
    public SetmealVO getById(Long id) {
        //套餐表查询
        SetmealVO setmealVO =setmealMapper.getById(id);
        //相关的setmeal_dish查询
        List<SetmealDish> setmealDishes=setMealDishMapper.getBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    @Override
    public void startOrStop(Integer status,Long id) {
        Setmeal setmeal= Setmeal.builder().
                status(status).
                id(id).
                build();
        if (status==1) {
            //套餐内如果有停售菜品，则不能上架
            Long count = dishMapper.countStatus(id);
            if (count > 0) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        }
        setmealMapper.update(setmeal);
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        //在售状态不能删除,计算停售的数量
      Long counted=setmealMapper.countByids(ids);
      if (counted>0)
      {
          throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
      }
        setmealMapper.deleteByIds(ids);
    }

    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);}

}
